package com.yunbiao.ybbusstopplate.utils.core.power;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;


import com.yunbiao.ybbusstopplate.base.APP;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

public class OutdoorPowerControl {
    private static final String TAG = "OutdoorPowerControl";
    //com.hsd.intelligence.outdoormachine.service.EnvironmentService
    private final String targetService = "com.hsd.intelligence.outdoormachine.service.EnvironmentService";
    private final String powerAction = "com.hsd.intelligence.outdoormachine.broadcast.SET_POWER";

    private static OutdoorPowerControl powerControl;

    private OutdoorPowerControl() {
    }

    public static OutdoorPowerControl getInstance() {
        if (powerControl == null) {
            powerControl = new OutdoorPowerControl();
        }
        return powerControl;
    }

    public boolean bordIsOutdoor() {
        boolean outdoor = false;

        ActivityManager manager = (ActivityManager) APP.Companion.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(Integer.MAX_VALUE);
        if (runningServices == null || runningServices.isEmpty()) {
            outdoor = false;
        } else {
            for (ActivityManager.RunningServiceInfo info : runningServices) {
                if (targetService.equals(info.service.getClassName())) {
                    outdoor = true;
                    break;
                }
            }
        }
        return outdoor;
    }

    public void setPower() {
        String power_on = PowerOffTool.getPowerOffTool().getPowerParam(PowerOffTool.POWER_ON);
        String power_off = PowerOffTool.getPowerOffTool().getPowerParam(PowerOffTool.POWER_OFF);
        if (TextUtils.isEmpty(power_on) || TextUtils.isEmpty(power_off)) {
            disEnablePower();
            return;
        }
        String[] powerOnArray = power_on.split(";");
        String runDate = powerOnArray[0];
        String powerOnTime = powerOnArray[1];

        String[] powerOffArray = power_off.split(";");
        String powerOffTime = powerOffArray[1];

        Log.d(TAG, "setPower: runDate->"+runDate);
        Integer[] runs = new Integer[7];
        for (int i = 0; i < runs.length; i++) {
            int week = i + 1;
            Log.d(TAG, "setPower: week->"+week);
            if (runDate.indexOf(week+"") != -1) {
                runs[i] = 1;
            } else {
                runs[i] = 0;
            }
        }
        enablePower(powerOnTime, powerOffTime, runs);
    }

    private void enablePower(String onTime, String offTime, Integer[] runs) {
        Log.d(TAG, "enablePower: ontime: " + onTime + ",offTime: " + offTime + ", run: " + runs.toString());
        try {
            Intent intent = new Intent();
            JSONObject powerInfo = new JSONObject();
            powerInfo.put("isOpen", true);//true表示开，false表示关
            powerInfo.put("powerOnTime", onTime);//开机时间
            powerInfo.put("powerOffTime", offTime);//关机时间
            //开关机策略，数组对应星期一 到 星期天
            //1代表执行，0代表不执行
//            List<Integer> powerDay = new ArrayList<>();
//            for (int i = 0; i < 7; i++) {
//                powerDay.add(1);
//            }
            List<Integer> powerDay = Arrays.asList(runs);
            powerInfo.put("powerDay", powerDay);//运行开关机策略

            Log.d(TAG, "enablePower: setpower->" + powerInfo.toString());
            intent.putExtra("setPower", powerInfo.toString());
            intent.setAction(powerAction);
            APP.Companion.getAppContext().sendBroadcast(intent);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void disEnablePower() {
        Log.d(TAG, "disEnablePower: ---------");
        try {
            Intent intent = new Intent();
            JSONObject powerInfo = new JSONObject();
            powerInfo.put("isOpen", false);//true表示开，false表示关
            intent.putExtra("setPower", powerInfo.toString());
            intent.setAction(powerAction);
            APP.Companion.getAppContext().sendBroadcast(intent);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
